package com.example.wechat.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.wechat.Adapter.Fg_Adapter;
import com.example.wechat.Fragment.Fg_fx;
import com.example.wechat.Fragment.Fg_txl;
import com.example.wechat.Fragment.Fg_wd;
import com.example.wechat.Fragment.Fg_weixin;
import com.example.wechat.R;

import java.util.ArrayList;
import java.util.List;

public class index extends AppCompatActivity {
    private ViewPager viewPager;
    private RadioButton weixin,txl,fx,wd;
    private RadioGroup radioGroup;
    private List<Fragment> fragment;
    private Fragment Fg_weixin,Fg_txl,Fg_fx,Fg_wd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        changeStatusBarTextImgColor(true);
        initView();
        function();
    }
    /**
     * 界面设置状态栏字体颜色
     */
    public void changeStatusBarTextImgColor(boolean isBlack) {
        if (isBlack) {
            //设置状态栏黑色字体
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            //恢复状态栏白色字体
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
    }

    //初始化控件
    public void initView() {
        viewPager=findViewById(R.id.view_pager);
        radioGroup=findViewById(R.id.btn);
        weixin=findViewById(R.id.weixin);
        txl=findViewById(R.id.txl);
        fx=findViewById(R.id.fx);
        wd=findViewById(R.id.my);
    }
    //功能
    public void function() {
        //创建四个Fragment对象
        Fg_weixin=new Fg_weixin();
        Fg_txl=new Fg_txl();
        Fg_fx=new Fg_fx();
        Fg_wd=new Fg_wd();
        //new一个ArrayList
        fragment=new ArrayList<>();
        fragment.add(Fg_weixin);
        fragment.add(Fg_txl);
        fragment.add(Fg_fx);
        fragment.add(Fg_wd);
        //创建Fragment的管理器
        FragmentManager manager=getSupportFragmentManager();
        //添加到viewpager中
        Fg_Adapter adapter=new Fg_Adapter(manager,fragment);
        viewPager.setAdapter(adapter);
        //设置viewpager选中第一个
        viewPager.setCurrentItem(0);
        /**
         * RadioButton绑定viewPager
         */
        //设置viewPager
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                switch (position)
                {
                    case 0:
                        radioGroup.check(R.id.weixin);
                        break;
                    case 1:
                        radioGroup.check(R.id.txl);
                        break;
                    case 2:
                        radioGroup.check(R.id.fx);
                        break;
                    case 3:
                        radioGroup.check(R.id.my);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //设置radioGroup
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i)
                {
                    case R.id.weixin:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.txl:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.fx:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.my:
                        viewPager.setCurrentItem(3);
                        break;
                }
            }
        });
    }
}
